package org.at;

import org.at.strategy.FullReductionStrategyFactory;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class ShoppingCart {
    private Map<Fruit, Integer> items = new HashMap<>();

    // 添加水果
    public void addFruit(Fruit fruit, int weight) {
        if (weight < 0) {
            throw new IllegalArgumentException("水果斤数不能为负数");
        }
        items.put(fruit, items.getOrDefault(fruit, 0) + weight);
    }

    public Map<Fruit, Integer> getItems() {
        return items;
    }

    // 清空购物车
    public void clear() {
        items.clear();
    }

    // 计算总支付金额
    public BigDecimal calculateDiscountedTotalPrice(String code) {
        BigDecimal total = BigDecimal.ZERO;
        for (Map.Entry<Fruit, Integer> entry : items.entrySet()) {
            Fruit fruit = entry.getKey();
            Integer weight = entry.getValue();
            total = total.add(fruit.getPrice().multiply(fruit.getDiscountRate()).multiply(BigDecimal.valueOf(weight)));

        }
        // 调用策略方法
        return FullReductionStrategyFactory.getStrategy(code).calculateDiscountedTotalPrice(total);
    }
}
